package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.entity.Class;
import com.project.entity.Department;
import com.project.entity.Major;
import com.project.myBeans.ResponseBean;
import com.project.service.ClassService;
import com.project.service.DepartmentService;
import com.project.service.MajorService;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WheelChen on 2017/6/22.
 * <p>
 * 系部操作
 */
@Controller
@RequestMapping("/department")
public class DepartmentController extends BaseService {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private ClassService classService;

    /**
     * author: WheelChen
     * date: 2017-6-22
     * function: 根据学院id查找系
     *
     * @param schoolId 学院id
     * @return
     */
    @RequestMapping(value = "/findBath/{schoolId}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean findBath(@PathVariable("schoolId") Integer schoolId) {
        ResponseBean responseBean = new ResponseBean();
        List<Department> departments;
        try {
            departments = departmentService.findBatchBySchoolId(schoolId);
            responseBean.setData(departments);
            responseBean.setSuccess(true);
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            return responseBean;
        }
    }

    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/8/3
     * function: 添加单个系
     *
     * @param department
     * @return
     */
    public ResponseBean addOne(@RequestBody Department department) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (departmentService.checkMajorIsExist(department)) {
                responseBean.setSuccess(false);
                responseBean.setMessage("该系已存在");
            } else {
                departmentService.addOne(department);
                responseBean.setSuccess(true);
                responseBean.setMessage("添加成功");
            }
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/findDetailBatch/{schoolId}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/8/3
     * function: 根据学院id查找学院下的系-专业-id
     *
     * @param schoolId
     * @return
     */
    public ResponseBean findDetailBatch(@PathVariable("schoolId") Integer schoolId) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.setData(departmentService.findDetailBatch(schoolId));
            responseBean.setSuccess(true);
            responseBean.setMessage("获取成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/findBatchByCondition", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/10/9
     * function: 根据系的大小分页并查出下属于的专业-班级
     *
     * @param department
     * @return
     */
    public ResponseBean findBatchByCondition(@RequestBody Department department){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (department.getPageIndex() > 0) department.setPageIndex(department.getPageIndex() - 1);
            List<Department> departmentList = departmentService.findBatchByCondition(department);
            List<JSONObject> jsonObjectList = new ArrayList<>();
            for (Department department1: departmentList){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("department", department1);
                //查处系下的专业
                List<Major> majorList = majorService.findBatch(department1.getId());
                //用于存储专业以及专业下班级
                List<JSONObject> majors = new ArrayList<>();
                for (Major major: majorList){
                    JSONObject jsonObject2 = new JSONObject();
                    jsonObject2.put("major", major);
                    List<Class> classList = classService.findBatchByMajorid(major.getId());
                    jsonObject2.put("classList", classList);
                    majors.add(jsonObject2);
                }
                jsonObject.put("majorList", majors);
                jsonObjectList.add(jsonObject);
            }
            responseBean.setData(jsonObjectList);
            responseBean.setTotal(department.getTotal());
            responseBean.setSuccess(true);
            responseBean.setMessage("获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }


}
